package com.laiketui.user.controller;

import com.laiketui.common.api.PubliceService;
import com.laiketui.core.annotation.ParamsMapping;
import com.laiketui.core.domain.Result;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.DictionaryConst;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.domain.vo.MainVo;
import com.laiketui.domain.vo.SaveAuthorizeUserInfoVo;
import com.laiketui.domain.vo.user.AlipayUserVo;
import com.laiketui.domain.vo.user.AppletsVo;
import com.laiketui.domain.vo.user.WxAuthPhoneVo;
import com.laiketui.root.annotation.HttpApiMethod;
import com.laiketui.user.api.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 登陆
 *
 * @author Trick
 * @date 2020/10/19 9:12
 */
@Api(tags = "登录")
@RestController
@Validated
public class LoginController {

    @Autowired
    LoginService loginService;

    @Autowired
    PubliceService publiceService;

    @ApiOperation("获取token")
    @PostMapping("/getToken")
    @HttpApiMethod(urlMapping = "app.login.token")
    public Result getToken(MainVo vo) {
        try {
            return Result.success(loginService.token(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("判断是否要注册")
    @PostMapping("is_register")
    public Result isRegister(MainVo vo) {
        try {
            Map<String, Object> result = loginService.isRegister(vo);
            return Result.success(result);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("授权未过期")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "微信openid", required = true, dataType = "int", paramType = "form")
    })
    @PostMapping("login_access")
    public Result loginAccess(MainVo vo, @NotNull String openid) {
        try {
            Map<String, Object> result = loginService.loginAccess(vo, openid);
            return Result.success(result);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("进入登陆页面")
    @PostMapping("loginIndex")
    @HttpApiMethod(urlMapping = "app.login.index")
    public Result loginIndex(MainVo vo) {
        try {
            Map<String, Object> result = loginService.loginIndex(vo);
            return Result.success(result);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("注册协议")
    @PostMapping("register_agreement")
    @HttpApiMethod(urlMapping = "app.login.register_agreement")
    public Result getRegisterAgreement(MainVo vo) {
        try {
            return Result.success(publiceService.getAgreement(vo.getStoreId(), DictionaryConst.AgreementType.AGREEMENTTYPE_REGISTER));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("隐私协议")
    @PostMapping("privacy_agreement")
    @HttpApiMethod(urlMapping = "app.login.privacy_agreement")
    public Result getPrivacyAgreement(MainVo vo) {
        try {
            return Result.success(publiceService.getAgreement(vo.getStoreId(), DictionaryConst.AgreementType.AGREEMENTTYPE_PRIVACY));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("忘记密码,重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "keyCode", value = "验证码", required = true, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "form")
    })
    @PostMapping("forgotPassword")
    @HttpApiMethod(urlMapping = "app.login.forgotPassword")
    public Result forgotPassword(MainVo vo, String phone, String keyCode, String password) {
        try {
            return Result.success(loginService.forgotPassword(vo, phone, keyCode, password));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("退出登陆")
    @PostMapping("quit")
    @HttpApiMethod(urlMapping = "app.login.quit")
    public Result quit(MainVo vo) {
        try {
            return Result.success(loginService.quit(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("小程序一键登录")
    @PostMapping("/appletsBindPhoneLogin")
    @HttpApiMethod(urlMapping = "app.login.appletsBindPhoneLogin")
    public Result appletsBindPhoneLogin(String code,MainVo vo){
        try{
            return Result.success(loginService.wxMiniBindPhoneLogin(code,vo));
        }catch (LaiKeAPIException e){
            return Result.error(e.getCode(),e.getMessage());
        }
    }

    @ApiOperation("小程序绑定openid")
    @PostMapping("appletsBindOpenid")
    @HttpApiMethod(urlMapping = "app.login.appletsBindOpenid")
    public Result appletsBindOpenid(String code,MainVo vo){
        try{
            return Result.success(loginService.wxBindOpenid(code,vo));
        }catch (LaiKeAPIException e){
            return Result.error(e.getCode(),e.getMessage());
        }
    }

    @ApiOperation("修改用户推荐人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "string", paramType = "form")
    })
    @PostMapping("chang_pid")
    @HttpApiMethod(urlMapping = "app.login.chang_pid")
    public Result changPid(MainVo vo, @ParamsMapping("pid") String pid) {
        try {
            loginService.changPid(vo, pid);
            return Result.success(GloabConst.ManaValue.MANA_VALUE_SUCCESS);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("获取小程序登录参数")
    @PostMapping("/getAppletParam")
    @HttpApiMethod(urlMapping = {"app.login.getAppletParam"})
    public Result getAppletParam(AppletsVo vo) {
        try {
            return Result.success(loginService.getAppletParam(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("小程序授权登录/注册")
    @PostMapping("/applets")
    @HttpApiMethod(urlMapping = {"app.login.user"})
    public Result applets(AppletsVo vo) {
        try {
            return Result.success(loginService.applets(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("【微信公众号】授权登录/注册")
    @PostMapping("/appletsWx")
    @HttpApiMethod(urlMapping = {"app.login.appletsWx"})
    public Result appletsWx(AppletsVo vo) {
        try {
            return Result.success(loginService.appletsWx(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("小程序授权获取用户手机")
    @PostMapping("/getWxPhone")
    @HttpApiMethod(urlMapping = {"app.login.getWxInfo"})
    public Result getWxPhone(WxAuthPhoneVo vo) {
        try {
            return Result.success(loginService.getWxPhone(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("小程序验证用户Token ")
    @PostMapping("/tokenAccess")
    @HttpApiMethod(urlMapping = {"app.login.login_access"})
    public Result tokenAccess(MainVo vo, @ParamsMapping("wx_id") String openid) {
        try {
            Map<String, Object> result = loginService.tokenAccess(vo, openid);
            return Result.success(result);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("获取微信appid")
    @PostMapping("/getWxAppId")
    @HttpApiMethod(urlMapping = {"app.login.getWxAppId"})
    public Result getWxAppId(MainVo vo) {
        try {
            return Result.success(loginService.getWxAppId(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("新增、修改阿里用户")
    @PostMapping("/aliUser")
    @HttpApiMethod(urlMapping = {"app.login.updateAliUser"})
    public Result synuser(AlipayUserVo vo) {
        try {
            return Result.success(loginService.aliUser(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("阿里用户登陆")
    @PostMapping("/aliUserLogin")
    @HttpApiMethod(urlMapping = {"app.login.mpaliUserLogin"})
    public Result aliUserLogin(MainVo vo, String alimp_auth_code) {
        try {

            return Result.success(loginService.aliUserLogin(vo, alimp_auth_code, true));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("阿里用户登陆")
    @PostMapping("/aliUserLoginApp")
    @HttpApiMethod(urlMapping = {"app.login.aliUserLoginApp"})
    public Result aliUserLoginApp(MainVo vo, String authCode) {
        try {
            return Result.success(loginService.aliUserLogin(vo, authCode, false));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("阿里用户登陆-app(非小程序场景)")
    @PostMapping("/aliUserLoginByApp")
    @HttpApiMethod(urlMapping = {"app.login.aliUserLoginByApp"})
    public Result aliUserLoginByApp(MainVo vo) {
        try {
            return Result.success(loginService.aliUserLoginByApp(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("阿里用户登陆-web(非小程序场景)")
    @PostMapping("/aliUserLoginByWeb")
    @HttpApiMethod(urlMapping = {"app.login.aliUserLoginByWeb"})
    public Result aliUserLoginByWeb(MainVo vo) {
        try {
            return Result.success(loginService.aliUserLoginByWeb(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("【微信公众号,APP】 用户绑定微信")
    @PostMapping("/bindWechat")
    @HttpApiMethod(urlMapping = {"app.user.bindWechat"})
    public Result bindWechat(AppletsVo vo) {
        try {
            loginService.bindWechat(vo);
            return Result.success(GloabConst.ManaValue.MANA_VALUE_SUCCESS);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }
}
